package com.simplelove.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.simplelove.entity.HealthCheck;
import com.simplelove.mapper.HealthCheckMapper;
import com.simplelove.service.HealthCheckService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 健康检查服务实现类
 */
@Service
public class HealthCheckServiceImpl extends ServiceImpl<HealthCheckMapper, HealthCheck> implements HealthCheckService {

    @Override
    public List<HealthCheck> syncHealthChecks(Long userId, List<HealthCheck> healthChecks) {
        for (HealthCheck healthCheck : healthChecks) {
            healthCheck.setUserId(userId);
            healthCheck.setUpdatedAt(LocalDateTime.now());
            
            if (healthCheck.getId() == null) {
                // 新增记录
                healthCheck.setCreatedAt(LocalDateTime.now());
                this.save(healthCheck);
            } else {
                // 更新记录
                this.updateById(healthCheck);
            }
        }
        return healthChecks;
    }

    @Override
    public List<HealthCheck> getUserHealthChecks(Long userId) {
        QueryWrapper<HealthCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                   .orderByDesc("last_check_date");
        return this.list(queryWrapper);
    }
}
